<?php

/*

COMP 353F
Dr. B Desai
Final Project
Group #11

6330746 Nicholas CONSTANTINIDIS
9532862 Jacqueline FU
5484537 Claudio Javier LOPEZ FLORES
9218416 Que Tung NGUYEN

*/

  session_start();
?>

<!DOCTYPE html>
<html lang="en">
<head>
        <title>CoBAGSys - Home</title>
        <link type="text/css" rel="stylesheet" href="css/mystyle.css">
</head>
<body>
    <?php
      require 'menus.php';
    ?>
    <?php
		function acceptBid($bid) {
			$db = new Connect();
			
			$sql = "UPDATE Transaction t, Posting p, Category c " .
				   "SET t.final = 1, " .
				   "t.revenue = (t.price * (SELECT n/100 FROM AdminSettings)) " .
				   " + c.fee " .
				   "WHERE t.pID = p.pID " .
				   "AND p.cID = c.cID " .
				   "AND t.tID = " . $bid;
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Couldn't accept bid
			{
				return false;
			}
			else // Bid accepted
			{
				return true;
			}
		}

		function updatePosting($trade) {
			$db = new Connect();
			
			$status = "Closed";
			if ($trade)
				$status = "Traded";
			
			$sql = "UPDATE Posting " .
				   "SET status = '" . $status . "' " .
				   "WHERE pID = " . $_GET['pid'];
			
			$result = $db->query($sql);
			
			if(!$result || mysql_affected_rows() == 0) // Couldn't update posting
			{
				return false;
			}
			else // Posting updated
			{
				return true;
			}
		}
				
		$acceptsuccess = false;
		if (isset($_GET['bid']))
			$acceptsuccess = acceptBid($_GET['bid']) && updatePosting(false);
		else if (isset($_GET['bbid']))
			$acceptsuccess = acceptBid($_GET['bbid']) && updatePosting(true);
    ?>
    <div id="main">
		<p class="title">
			Accept bid
		</p>
<?php
	if ($acceptsuccess)
	{
		print "<p class=\"normal\">The bid has been accepted!</p>";
	}
	else
	{
		print "<p></p><span class=\"error\">INVALID BID SPECIFIED!</span>";
	}
?>
    </div>
</body>
</html>
